﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace IHISCETeim.Library
{
    /// <summary>
    /// 空值处理
    /// </summary>
    public class Null
    {
        public static short NullShort
        {
            get { return -1; }
        }

        public static int NullInteger
        {
            get { return -1; }
        }

        public static byte NullByte
        {
            get { return 255; }
        }

        public static float NullSingle
        {
            get { return float.MinValue; }
        }

        public static double NullDouble
        {
            get { return double.MinValue; }
        }

        public static decimal NullDecimal
        {
            get { return decimal.MinValue; }
        }

        public static System.DateTime NullDate
        {
            get { return System.DateTime.MinValue; }
        }

        public static string NullString
        {
            get { return ""; }
        }

        public static bool NullBoolean
        {
            get { return false; }
        }

        public static Guid NullGuid
        {
            get { return Guid.Empty; }
        }

      
        /// <summary>
        /// 将数据库中相应类型空值(null)转换成.net类型值
        /// </summary>
        /// <param name="objValue"></param>
        /// <param name="objField"></param>
        /// <returns></returns>
        public static object SetNull(object objValue, object objField)
        {
            object functionReturnValue = null;
            if (objValue == System.DBNull.Value)
            {
                if (objField is short)
                {
                    functionReturnValue = NullShort;
                }
                else if (objField is byte)
                {
                    functionReturnValue = NullByte;
                }
                else if (objField is int)
                {
                    functionReturnValue = NullInteger;
                }
                else if (objField is float)
                {
                    functionReturnValue = NullSingle;
                }
                else if (objField is double)
                {
                    functionReturnValue = NullDouble;
                }
                else if (objField is decimal)
                {
                    functionReturnValue = NullDecimal;
                }
                else if (objField is System.DateTime)
                {
                    functionReturnValue = NullDate;
                }
                else if (objField is string)
                {
                    functionReturnValue = NullString;
                }
                else if (objField is bool)
                {
                    functionReturnValue = NullBoolean;
                }
                else if (objField is Guid)
                {
                    functionReturnValue = NullGuid;
                }
                else
                {
                    // complex object 
                    functionReturnValue = null;
                }
            }
            else
            {
                // return value 
                functionReturnValue = objValue;
            }
            return functionReturnValue;
        }

       
        /// <summary>
        /// 用于设置反射类型相应类型空值
        /// </summary>
        /// <param name="objPropertyInfo"></param>
        /// <returns></returns>
        public static object SetNull(PropertyInfo objPropertyInfo)
        {
            object functionReturnValue = null;
            switch (objPropertyInfo.PropertyType.ToString())
            {
                case "System.Int16":
                    functionReturnValue = NullShort;
                    break;
                case "System.Int32":
                case "System.Int64":
                    functionReturnValue = NullInteger;
                    break;
                case "system.Byte":
                    functionReturnValue = NullByte;
                    break;
                case "System.Single":
                    functionReturnValue = NullSingle;
                    break;
                case "System.Double":
                    functionReturnValue = NullDouble;
                    break;
                case "System.Decimal":
                    functionReturnValue = NullDecimal;
                    break;
                case "System.DateTime":
                    functionReturnValue = System.DBNull.Value;
                    break;
                case "System.String":
                case "System.Char":
                    functionReturnValue = NullString;
                    break;
                case "System.Boolean":
                    functionReturnValue = NullBoolean;
                    break;
                case "System.Guid":
                    functionReturnValue = NullGuid;
                    break;
                default:
                    // Enumerations default to the first entry 
                    Type pType = objPropertyInfo.PropertyType;
                    if (pType.BaseType.Equals(typeof(System.Enum)))
                    {
                        System.Array objEnumValues = System.Enum.GetValues(pType);
                        Array.Sort(objEnumValues);
                        functionReturnValue = System.Enum.ToObject(pType, objEnumValues.GetValue(0));
                    }
                    else
                    {
                        // complex object 
                        functionReturnValue = null;
                    }

                    break;
            }
            return functionReturnValue;
        }

      

        public static bool SetNullBoolean(object objValue)
        {
            bool retValue = Null.NullBoolean;
            if (!(objValue == System.DBNull.Value))
            {
                retValue = Convert.ToBoolean(objValue);
            }
            return retValue;
        }

        public static DateTime SetNullDateTime(object objValue)
        {
            DateTime retValue = Null.NullDate;
            if (!(objValue == System.DBNull.Value))
            {
                retValue = Convert.ToDateTime(objValue);
            }
            return retValue;
        }

        public static int SetNullInteger(object objValue)
        {
            int retValue = Null.NullInteger;
            if (!(objValue == System.DBNull.Value))
            {
                retValue = Convert.ToInt32(objValue);
            }
            return retValue;
        }

        public static float SetNullSingle(object objValue)
        {
            float retValue = Null.NullSingle;
            if (!(objValue == System.DBNull.Value))
            {
                retValue = Convert.ToSingle(objValue);
            }
            return retValue;
        }
        public static string SetNullString(object objValue)
        {
            string retValue = Null.NullString;
            if (!(objValue == System.DBNull.Value))
            {
                retValue = Convert.ToString(objValue);
            }
            return retValue;
        }

        /// <summary>
        /// 将.net类型空值转换成数据库的空值(null)
        /// </summary>
        /// <param name="objField"></param>
        /// <returns></returns>
        public static object GetNull(object objField)
        {
            return GetNull(objField, System.DBNull.Value);
        }
        /// <summary>
        /// 将.net类型空值转换成数据库的空值(null)
        /// </summary>
        /// <param name="objField"></param>
        /// <param name="objDBNull"></param>
        /// <returns></returns>
        public static object GetNull(object objField, object objDBNull)
        {
            object functionReturnValue = null;
            functionReturnValue = objField;
            if (objField == null)
            {
                functionReturnValue = objDBNull;
            }
            else if (objField is byte)
            {
                if (Convert.ToByte(objField) == NullByte)
                {
                    functionReturnValue = objDBNull;
                }
            }
            else if (objField is short)
            {
                if (Convert.ToInt16(objField) == NullShort)
                {
                    functionReturnValue = objDBNull;
                }
            }
            else if (objField is int)
            {
                if (Convert.ToInt32(objField) == NullInteger)
                {
                    functionReturnValue = objDBNull;
                }
            }
            else if (objField is float)
            {
                if (Convert.ToSingle(objField) == NullSingle)
                {
                    functionReturnValue = objDBNull;
                }
            }
            else if (objField is double)
            {
                if (Convert.ToDouble(objField) == NullDouble)
                {
                    functionReturnValue = objDBNull;
                }
            }
            else if (objField is decimal)
            {
                if (Convert.ToDecimal(objField) == NullDecimal)
                {
                    functionReturnValue = objDBNull;
                }
            }
            else if (objField is System.DateTime)
            {
                // compare the Date part of the DateTime with the DatePart of the NullDate ( this avoids subtle time differences ) 
                if (Convert.ToDateTime(objField).Date == NullDate.Date)
                {
                    functionReturnValue = objDBNull;
                }
            }
            else if (objField is string)
            {
                if (objField == null)
                {
                    functionReturnValue = objDBNull;
                }
                else
                {
                    if (objField.ToString() == NullString)
                    {
                        functionReturnValue = objDBNull;
                    }
                }
            }
            else if (objField is bool)
            {
                if (Convert.ToBoolean(objField) == NullBoolean)
                {
                    functionReturnValue = objDBNull;
                }
            }
            else if (objField is Guid)
            {
                if (((System.Guid)objField).Equals(NullGuid))
                {
                    functionReturnValue = objDBNull;
                }
            }
            return functionReturnValue;
        }

        // checks if a field contains an application encoded null value 
        public static bool IsNull(object objField)
        {
            bool functionReturnValue = false;
            if ((objField != null))
            {
                if (objField is int)
                {
                    functionReturnValue = objField.Equals(NullInteger);
                }
                else if (objField is short)
                {
                    functionReturnValue = objField.Equals(NullShort);
                }
                else if (objField is byte)
                {
                    functionReturnValue = objField.Equals(NullByte);
                }
                else if (objField is float)
                {
                    functionReturnValue = objField.Equals(NullSingle);
                }
                else if (objField is double)
                {
                    functionReturnValue = objField.Equals(NullDouble);
                }
                else if (objField is decimal)
                {
                    functionReturnValue = objField.Equals(NullDecimal);
                }
                else if (objField is System.DateTime)
                {
                    DateTime objDate = (DateTime)objField;
                    functionReturnValue = objDate.Date.Equals(NullDate.Date);
                }
                else if (objField is string)
                {
                    functionReturnValue = objField.Equals(NullString);
                }
                else if (objField is bool)
                {
                    functionReturnValue = objField.Equals(NullBoolean);
                }
                else if (objField is Guid)
                {
                    functionReturnValue = objField.Equals(NullGuid);
                }
                else
                {
                    // complex object 
                    functionReturnValue = false;
                }
            }
            else
            {
                functionReturnValue = true;
            }
            return functionReturnValue;
        }

    } 
}
